{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#PROGRAMMING_FEVER\n",
    "#pravee\n",
    "import cv2\n",
    "import numpy as np\n",
    "def draw_rectangle(event, x, y, flags, params):\n",
    "    global x_init, y_init, drawing, top_left_pt, bottom_right_pt\n",
    "    if event == cv2.EVENT_LBUTTONDOWN:\n",
    "        drawing = True\n",
    "        x_init, y_init = x, y\n",
    "    elif event == cv2.EVENT_MOUSEMOVE:\n",
    "        if drawing:\n",
    "            top_left_pt = (min(x_init, x), min(y_init, y))\n",
    "            bottom_right_pt = (max(x_init, x), max(y_init, y))\n",
    "            img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x]\n",
    "    elif event == cv2.EVENT_LBUTTONUP:\n",
    "        drawing = False\n",
    "        top_left_pt = (min(x_init, x), min(y_init, y))\n",
    "        bottom_right_pt = (max(x_init, x), max(y_init, y))\n",
    "        img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x]\n",
    "if __name__=='__main__':\n",
    "    drawing = False\n",
    "    top_left_pt, bottom_right_pt = (-1,-1), (-1,-1)\n",
    "    cap = cv2.VideoCapture(0)\n",
    "    if not cap.isOpened():\n",
    "        raise IOError(\"Cannot open webcam\")\n",
    "    cv2.namedWindow('Webcam')\n",
    "    cv2.setMouseCallback('Webcam', draw_rectangle)\n",
    "    while True:\n",
    "        ret, frame = cap.read()\n",
    "        img = cv2.resize(frame, None, fx=0.5, fy=0.5,interpolation=cv2.INTER_AREA)\n",
    "        (x0,y0), (x1,y1) = top_left_pt, bottom_right_pt\n",
    "        img[y0:y1, x0:x1] = 255 - img[y0:y1, x0:x1]\n",
    "        cv2.imshow('Webcam', img)\n",
    "        c = cv2.waitKey(1)\n",
    "        if c == 27:\n",
    "            break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
